// packageA/mycomponent/goodsComponent/plugin/addressOption/addressOption.js
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    plugin: {
      type: null
    },
    goods_info: {
      type: null
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    addressName: "",
    showAddressList: false,
    yzaddressInfo: [],
    state: 1, //1就是无货，2就是有货,
    judgmentOptionId: null
  },
  lifetimes: {
    attached() {
      this.txtComputed()
      if (this.properties.plugin) {
        this.firstGetData();
      }
    },
    ready() {},
    detached() {

    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    showAddressShow() {
      this.setData({
        showAddressList: true
      })
      console.log(this.data.showAddressList, 'aaaaaaaaa')
    },
    confirmSelectAddress(e) {
      let item = e.detail;
      this.setData({
        yzaddressInfo:item
      })
      let addressq = `${this.data.yzaddressInfo.province}${this.data.yzaddressInfo.city}${this.data.yzaddressInfo.district}${this.data.yzaddressInfo.street}${this.data.yzaddressInfo.address}`;
      this.setData({
        addressName:addressq,
        showAddressList:false
      })
      this.getData();
      this.tapMrAdress(item);
    },
    // 选中后设置成默认地址
    tapMrAdress(item){
      let json = {
        username: item.username,
        mobile: item.mobile,
        province: item.province,
        city: item.city,
        district: item.district,
        address: item.address,
        isdefault: 1,
        address_id: item.id
      };
      let urlStr = app.getNetAddresss("member.member-address.update");
      app._postNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          var res = resdata.data;
          if (res.result == 1) {
            
          }
        },
      });
    },
    txtComputed() {
      // 默认的情况下显示 默认地址
      let addressq = "";
      if (this.data.plugin.member_address) {
        this.data.plugin.member_address.forEach(element => {
          if (element.isdefault == 1) {
            addressq = `${element.province}${element.city}${element.district}${element.street}${element.address}`;
          }
        });
        this.setData({
          addressName: addressq
        })
        console.log(this.data.plugin, this.addressName, 'aaaaaaaa1111')
      }
    },
    firstGetData() {
      let arr = [];
      if(this.data.plugin.member_address.length > 0){
        this.data.plugin.member_address.forEach(element => {
          if (element.isdefault == 1) {
            arr = element;
          }
        });
        if (!this.judgmentOptionId) {
          this.setData({
            judgmentOptionId: this.data.goods_info.get_goods.has_many_options[0].id
          })
        }
        this.setData({
          yzaddressInfo: arr
        })
        this.getData();
      }
    },
    getData() {
      let arr = this.data.yzaddressInfo;
      let json = {
        goods_id: this.data.goods_info.get_goods.id,
        goods_option_id: this.data.judgmentOptionId, //默认第一个 this.goods_info.get_goods.has_many_options[0].id
        username: arr.username,
        mobile: arr.mobile,
        province: arr.province,
        city: arr.city,
        district: arr.district,
        address: arr.address
      };
      let urlStr = app.getNetAddresss("plugin.jd-supply.frontend.goods.option.inStock");
      app._postNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          var res = resdata.data;
          if (res.result == 1) {
            this.setData({
              state:res.data.status
            })
            this.triggerEvent('addressState',this.data.state)
          }
        },
      });
    }
  }
})